// @ts-nocheck
import axios from 'axios'
import router from '@/router/index'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import config from "@/config/index"
import { showToast } from 'vant'

console.log(import.meta)
const host1 = config[import.meta.env.MODE].baseUrl
// const host2 = config[import.meta.env.MODE].contractUrl
const service = axios.create({
    baseURL: host1,
    timeout: 100000 // request timeout
})

service.interceptors.request.use((options) => {
   
    if (options.method == 'get') {
        if (!(options && options.params && options.params.noCover)) {
            document.getElementById("wholeCover").style.display = "block"
        }
    } else if (options.method == 'post') {
        if (!(options && options.data && options.data.noCover)) {
            document.getElementById("wholeCover").style.display = "block"
        }
    }
    let baseURL = host1
    

    const headers = {
        "Content-Type": "application/json",
        //TODO 待封装
        LoginUser: window.userInfo && window.userInfo.token
    };
    return {
        ...options,
        headers,
        baseURL
    };
});


// 请求拦截器，内部根据返回值，重新组装，统一管理。
service.interceptors.response.use(res => {

    // if (typeof res.data !== 'object') {
    //     console.log('服务端异常')
    //     return Promise.reject(res)
    // }
    //TODO 待验证
    if (res.data.code != 1) {
        // if (res.data.msg.indexOf("10")<0) {
        //   console.log(res.data.msg)
        //   Toast(res.data.msg)
        // }
        if (res.data.code == 1002) {
            router.push({ path: "/login" })
        }

        if (res.data.code == 0 || res.data.code == 1005) {
            showToast(res.data.msg)
        }
        return Promise.reject(res.data)
    }
    // console.log("返回的数据", res.data.data)
    return res.data.data
})

export default service